﻿#include <iostream>
#include <unordered_set>
using namespace std;

class GCObject {
public:
    bool marked = false;
    virtual ~GCObject() {}
};

unordered_set<GCObject*> heap;

void gc_mark(GCObject* obj) {
    if (!obj || obj->marked) return;
    obj->marked = true;
    // 此处可以递归标记子对象（省略）
}

void gc_sweep() {
    for (auto it = heap.begin(); it != heap.end();) {
        if (!(*it)->marked) {
            delete *it;
            it = heap.erase(it);
        } else {
            (*it)->marked = false;
            ++it;
        }
    }
}

class MyObject : public GCObject {
public:
    MyObject(string name) : name_(name) {
        cout << "Created: " << name_ << endl;
    }
    ~MyObject() {
        cout << "Destroyed: " << name_ << endl;
    }
private:
    string name_;
};

int main() {
    GCObject* a = new MyObject("A");
    GCObject* b = new MyObject("B");
    heap.insert(a);
    heap.insert(b);

    // 模拟保留 a、释放 b
    gc_mark(a);
    gc_sweep(); // 只销毁未标记的 b
}
